/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.transaction;

/**
 * Common representation of the current state of a transaction.
 * Serves as base interface for {@link TransactionStatus} as well as
 * {@link ReactiveTransaction}.
 *
 * @author Juergen Hoeller
 * @since 5.2
 */
/*
1 TransactionExecution 接口总结
TransactionExecution 是 Spring 事务框架中表示事务执行状态的通用接口
定义了事务的基本状态查询和控制方法，作为同步和响应式事务的统一抽象
为 TransactionStatus 和 ReactiveTransaction 提供公共基础接口
2 内部成员说明
	方法
	boolean isNewTransaction(): 返回当前事务是否为新创建的事务，用于区分新事务和参与现有事务的情况
	void setRollbackOnly(): 将事务标记为仅回滚状态，指示事务管理器只能回滚该事务
	boolean isRollbackOnly(): 检查事务是否已被标记为仅回滚状态（应用或基础设施标记）
	boolean isCompleted(): 判断事务是否已完成（已提交或已回滚）
3 类关系结构
	TransactionExecution (interface)
	├── TransactionStatus (interface)
	│   └── AbstractTransactionStatus (class)
	│       ├── SimpleTransactionStatus (class)
	│       └── DefaultTransactionStatus (class)
	└── ReactiveTransaction (interface)
		└── GenericReactiveTransaction (class)

	相关类和接口区别说明
		TransactionExecution: 基础事务执行状态接口，定义了事务的基本状态查询和控制方法，作为同步和响应式事务的统一抽象
		TransactionStatus: 传统同步事务的状态接口，扩展了 TransactionExecution，添加了对挂起/恢复等同步事务特有功能的支持
		ReactiveTransaction: 响应式事务的状态接口，扩展了 TransactionExecution，适用于响应式编程环境
		AbstractTransactionStatus: 抽象基类，实现了 TransactionStatus 接口的通用逻辑
		SimpleTransactionStatus: 简单的事务状态实现，用于基本场景
		DefaultTransactionStatus: 默认的事务状态实现，提供了完整的事务状态管理功能
		GenericReactiveTransaction: 通用的响应式事务实现，支持响应式编程模式

4 使用场景
	在事务管理器中跟踪事务执行状态
	应用代码中检查事务状态或标记事务为回滚
	作为统一接口处理同步和响应式事务状态
	在事务拦截器和切面中管理事务生命周期
5 实际代码应用示例
// 在事务拦截器中使用
public void commitTransactionAfterReturning(TransactionInfo txInfo) {
    if (txInfo != null && txInfo.getTransactionStatus() != null) {
        // 检查事务是否已完成
        if (!txInfo.getTransactionStatus().isCompleted()) {
            // 提交事务
            txInfo.getTransactionManager().commit(txInfo.getTransactionStatus());
        }
    }
}

// 在业务代码中使用
public void businessMethod() {
    TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
    try {
        // 业务逻辑
        doBusinessWork();

        // 检查是否为新事务
        if (status.isNewTransaction()) {
            // 只有新事务才执行特定逻辑
        }
    } catch (Exception e) {
        // 标记为回滚
        status.setRollbackOnly();
        throw e;
    } finally {
        // 检查是否标记为回滚
        if (status.isRollbackOnly()) {
            // 处理回滚逻辑
        }
    }
}

 */
public interface TransactionExecution {

	/**
	 * Return whether the present transaction is new; otherwise participating
	 * in an existing transaction, or potentially not running in an actual
	 * transaction in the first place.
	 */
	boolean isNewTransaction();

	/**
	 * Set the transaction rollback-only. This instructs the transaction manager
	 * that the only possible outcome of the transaction may be a rollback, as
	 * alternative to throwing an exception which would in turn trigger a rollback.
	 */
	void setRollbackOnly();

	/**
	 * Return whether the transaction has been marked as rollback-only
	 * (either by the application or by the transaction infrastructure).
	 */
	boolean isRollbackOnly();

	/**
	 * Return whether this transaction is completed, that is,
	 * whether it has already been committed or rolled back.
	 */
	boolean isCompleted();

}
